package com.cmq.web_1.api_test;

import java.util.*;
import java.util.stream.Collectors;

public class StudentService {
    // 模拟数据库 - 使用内存存储
    private static Map<String, Student> studentDatabase = new HashMap<>();

    // 静态初始化块 - 初始化示例数据
    static {
        initializeSampleData();
    }

    // 初始化示例数据
    private static void initializeSampleData() {
        Student[] students = {
                new Student("2023001", "张三", 20, "男", "计算机1班", "计算机科学", "zhangsan@edu.cn"),
                new Student("2023002", "李四", 19, "女", "计算机1班", "计算机科学", "lisi@edu.cn"),
                new Student("2023003", "王五", 21, "男", "计算机2班", "计算机科学", "wangwu@edu.cn"),
                new Student("2023004", "赵六", 20, "女", "软件1班", "软件工程", "zhaoliu@edu.cn"),
                new Student("2023005", "钱七", 22, "男", "软件1班", "软件工程", "qianqi@edu.cn"),
                new Student("2023006", "孙八", 19, "女", "网络1班", "网络工程", "sunba@edu.cn"),
                new Student("2023007", "周九", 20, "男", "网络1班", "网络工程", "zhoujiu@edu.cn"),
                new Student("2023008", "吴十", 21, "女", "计算机2班", "计算机科学", "wushi@edu.cn"),
                new Student("2023009", "郑十一", 20, "男", "软件2班", "软件工程", "zhengshiyi@edu.cn"),
                new Student("2023010", "王十二", 19, "女", "网络2班", "网络工程", "wangshier@edu.cn")
        };

        for (Student student : students) {
            studentDatabase.put(student.getId(), student);
        }
    }

    // 获取所有学生
    public List<Student> getAllStudents() {
        return new ArrayList<>(studentDatabase.values());
    }

    // 根据学号查询学生
    public Student getStudentById(String id) {
        return studentDatabase.get(id);
    }

    // 根据姓名查询学生（支持模糊查询）
    public List<Student> getStudentsByName(String name) {
        return studentDatabase.values().stream()
                .filter(student -> student.getName().toLowerCase().contains(name.toLowerCase()))
                .collect(Collectors.toList());
    }

    // 根据班级查询学生
    public List<Student> getStudentsByClass(String className) {
        return studentDatabase.values().stream()
                .filter(student -> student.getClassName().equalsIgnoreCase(className))
                .collect(Collectors.toList());
    }

    // 根据专业查询学生
    public List<Student> getStudentsByMajor(String major) {
        return studentDatabase.values().stream()
                .filter(student -> student.getMajor().equalsIgnoreCase(major))
                .collect(Collectors.toList());
    }

    // 根据性别查询学生
    public List<Student> getStudentsByGender(String gender) {
        return studentDatabase.values().stream()
                .filter(student -> student.getGender().equals(gender))
                .collect(Collectors.toList());
    }

    // 多条件组合查询
    public List<Student> searchStudents(Map<String, String> conditions) {
        return studentDatabase.values().stream()
                .filter(student -> {
                    boolean match = true;
                    if (conditions.containsKey("id") && !conditions.get("id").isEmpty()) {
                        match = match && student.getId().equals(conditions.get("id"));
                    }
                    if (conditions.containsKey("name") && !conditions.get("name").isEmpty()) {
                        match = match && student.getName().toLowerCase()
                                .contains(conditions.get("name").toLowerCase());
                    }
                    if (conditions.containsKey("className") && !conditions.get("className").isEmpty()) {
                        match = match && student.getClassName().equalsIgnoreCase(conditions.get("className"));
                    }
                    if (conditions.containsKey("major") && !conditions.get("major").isEmpty()) {
                        match = match && student.getMajor().equalsIgnoreCase(conditions.get("major"));
                    }
                    if (conditions.containsKey("gender") && !conditions.get("gender").isEmpty()) {
                        match = match && student.getGender().equals(conditions.get("gender"));
                    }
                    return match;
                })
                .collect(Collectors.toList());
    }

    // 获取所有班级列表
    public List<String> getAllClasses() {
        return studentDatabase.values().stream()
                .map(Student::getClassName)
                .distinct()
                .sorted()
                .collect(Collectors.toList());
    }

    // 获取所有专业列表
    public List<String> getAllMajors() {
        return studentDatabase.values().stream()
                .map(Student::getMajor)
                .distinct()
                .sorted()
                .collect(Collectors.toList());
    }

    // 获取学生总数
    public int getStudentCount() {
        return studentDatabase.size();
    }

    // 添加学生（用于扩展）
    public boolean addStudent(Student student) {
        if (studentDatabase.containsKey(student.getId())) {
            return false; // 学号已存在
        }
        studentDatabase.put(student.getId(), student);
        return true;
    }
}